<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<th:block th:fragment="navbar">
    <nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
        <img th:src="@{/static/img/logo.png}"
             alt="logo" height="50" width="50">
        <a class="navbar-brand text-success" th:href="@{/index}">人民大饭堂</a>
        <button class="navbar-toggler collapsed" type="button" data-toggle="collapse" data-target="#navbars"
                aria-controls="navbars" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="navbar-collapse collapse" id="navbars">
            <ul class="nav ml-auto">
                <li class="nav-item active">
                    <a class="nav-link text-success" th:href="@{/index}">首页 <span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item active">
                    <a class="nav-link text-success" th:href="@{/news}">资讯 </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link text-success" href="#" th:onclick="goCart()">购物车</a>
                </li>
                <li class="nav-item dropdown" th:if="${session.logined}">
                    <a class="nav-link dropdown-toggle text-success" href="#" id="dropdown" data-toggle="dropdown"
                       aria-haspopup="true" aria-expanded="false">[[${session.user} == null ? '' : ${session.user.username}]]</a>
                    <div class="dropdown-menu bg-white" aria-labelledby="dropdown">
                        <a class="dropdown-item text-success small" th:href="@{/user/personal}">个人中心</a>
                        <a class="dropdown-item text-success small" th:href="@{/order}">我的订单</a>
                        <a class="dropdown-item text-success small" th:onclick="doLogout()">退出登录</a>
                    </div>
                </li>
                <li class="nav-item" th:unless="${session.logined}">
                    <a class="nav-link text-success" href="" data-toggle="modal" data-target="#login">登录/注册</a>
                </li>
            </ul>
        </div>
    </nav>
    <!-- 登录窗口 -->
    <div id="login" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-body">
                    <button class="close" data-dismiss="modal">
                        <span>×</span>
                    </button>
                </div>
                <div class="modal-title">
                    <h5 class="text-center">登录</h5>
                </div>
                <div class="modal-body">
                    <form class="form-group">
                        <div class="form-group">
                            <label for="lg-username">用户名</label>
                            <input id="lg-username" name="username" class="form-control" type="text" placeholder="请输入用户名" required>
                        </div>
                        <div class="form-group">
                            <label for="lg-password">密码</label>
                            <input id="lg-password" name="password" class="form-control" type="password" placeholder="请输入密码" required>
                        </div>
                        <div class="text-right">
                            <button class="btn btn-primary" type="button" th:onclick="doLogin()">登录</button>
                            <button class="btn btn-danger" data-dismiss="modal">取消</button>
                        </div>
                        <a href="" data-toggle="modal" data-dismiss="modal" data-target="#register">还没有账号？点我注册</a>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <!-- 注册窗口 -->
    <div id="register" class="modal fade" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-body">
                    <button class="close" data-dismiss="modal">
                        <span>×</span>
                    </button>
                </div>
                <div class="modal-title">
                    <h5 class="text-center">注册</h5>
                </div>
                <div class="modal-body">
                    <form class="form-group">
                        <div class="form-group">
                            <label for="rg-username">用户名</label>
                            <input id="rg-username" name="username" class="form-control" type="text" placeholder="6-15位字母或数字" required>
                        </div>
                        <div class="form-group">
                            <label for="rg-password">密码</label>
                            <input id="rg-password" name="password" class="form-control" type="password" placeholder="至少6位字母或数字"
                                   required>
                        </div>
                        <div class="form-group">
                            <label for="rg-password2">再次输入密码</label>
                            <input id="rg-password2" name="passwordAgain" class="form-control" type="password" placeholder="至少6位字母或数字"
                                   required>
                        </div>
                        <div class="text-right">
                            <button class="btn btn-primary" type="button" th:onclick="doRegister()">提交</button>
                            <button class="btn btn-danger" data-dismiss="modal">取消</button>
                        </div>
                        <a href="" data-toggle="modal" data-dismiss="modal" data-target="#login">已有账号？点我登录</a>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript" th:inline="javascript">
        goCart = () => {
            let logined = [[${session.logined eq true}]]
            if (logined) {
                location.href = [[@{/cart}]];
            } else {
                $('#login').modal()
            }
        }
    </script>
</th:block>
</html>